/**
 * 频道独立的模块
 */
import { getChannels, getAllChannels } from '@/api/channels'
const state = {
  userChannel: [],
  AllChannel: []
}
const mutations = {
  setUserChannel(state, value) {
    state.userChannel = value
  },
  setAllChannel(state, value) {
    state.AllChannel = value
  },
  // 给用户添加频道
  addUserChannel(state, value) {
    state.userChannel.push(value)
  }
}
const actions = {
  async ActionGetUserChannel(context) {
    const res = await getChannels()
    context.commit('setUserChannel', res.data.channels)
  },
  async ActionGetAllChannel(context) {
    const res = await getAllChannels()
    context.commit('setAllChannel', res.data.channels)
  }
}
const getters = {
  // 计算可选频道
  optionChannel(state) {
    return state.AllChannel.filter(
      item => !state.userChannel.some(channel => channel.id === item.id)
    )
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
